package com.tbvtc.agriculturalcreditchainserver2025.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.websocket.server.ServerContainer;

@Configuration
public class WebSocketConfiguration {

    @Bean
    // 仅在Web应用环境中生效
    @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
    // 确保存在ServerContainer类（真实容器或模拟容器都可能满足）
    @ConditionalOnClass(ServerContainer.class)
    // 排除测试环境（检测Spring测试框架的核心类）
    @ConditionalOnMissingClass("org.springframework.test.context.junit.jupiter.SpringExtension")
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}